package com.ruoyi.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.QualityContinuousImprovement;
import com.ruoyi.quality.domain.bo.QualityContinuousImprovementBo;
import com.ruoyi.quality.domain.vo.QualityContinuousImprovementVo;
import com.ruoyi.quality.mapper.QualityContinuousImprovementMapper;
import com.ruoyi.quality.service.IQualityContinuousImprovementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 持续改善Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@RequiredArgsConstructor
@Service
public class QualityContinuousImprovementServiceImpl implements IQualityContinuousImprovementService {

    private final QualityContinuousImprovementMapper baseMapper;

    /**
     * 查询持续改善
     */
    @Override
    public QualityContinuousImprovementVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询持续改善
     */
    @Override
    public QualityContinuousImprovementVo queryByProcessInstanceId(String processInstanceId){
        QualityContinuousImprovementBo bo = new QualityContinuousImprovementBo();
        bo.setProcessInstanceId(processInstanceId);
        LambdaQueryWrapper<QualityContinuousImprovement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询持续改善列表
     */
    @Override
    public TableDataInfo<QualityContinuousImprovementVo> queryPageList(QualityContinuousImprovementBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QualityContinuousImprovement> lqw = buildQueryWrapper(bo);
        Page<QualityContinuousImprovementVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询持续改善列表
     */
    @Override
    public List<QualityContinuousImprovementVo> queryList(QualityContinuousImprovementBo bo) {
        LambdaQueryWrapper<QualityContinuousImprovement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QualityContinuousImprovement> buildQueryWrapper(QualityContinuousImprovementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QualityContinuousImprovement> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProcessInstanceId()), QualityContinuousImprovement::getProcessInstanceId, bo.getProcessInstanceId());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartment()), QualityContinuousImprovement::getDepartment, bo.getDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), QualityContinuousImprovement::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getImprovement()), QualityContinuousImprovement::getImprovement, bo.getImprovement());
        lqw.eq(StringUtils.isNotBlank(bo.getAudit()), QualityContinuousImprovement::getAudit, bo.getAudit());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), QualityContinuousImprovement::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getSuggestions()), QualityContinuousImprovement::getSuggestions, bo.getSuggestions());
        lqw.eq(StringUtils.isNotBlank(bo.getEffect()), QualityContinuousImprovement::getEffect, bo.getEffect());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), QualityContinuousImprovement::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPerson()), QualityContinuousImprovement::getPerson, bo.getPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getAttach()), QualityContinuousImprovement::getAttach, bo.getAttach());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), QualityContinuousImprovement::getLevel, bo.getLevel());
        lqw.eq(bo.getTotalScore() != null, QualityContinuousImprovement::getTotalScore, bo.getTotalScore());
        lqw.eq(bo.getReview() != null, QualityContinuousImprovement::getReview, bo.getReview());
        lqw.eq(bo.getSharing() != null, QualityContinuousImprovement::getSharing, bo.getSharing());
        lqw.eq(StringUtils.isNotBlank(bo.getAllDepartment()), QualityContinuousImprovement::getAllDepartment, bo.getAllDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getResolveAttachments()), QualityContinuousImprovement::getResolveAttachments, bo.getResolveAttachments());
        lqw.eq(bo.getDate() != null, QualityContinuousImprovement::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSchedule()), QualityContinuousImprovement::getSchedule, bo.getSchedule());
        lqw.eq(bo.getUserId() != null, QualityContinuousImprovement::getUserId, bo.getUserId());
        lqw.eq(bo.getGroupId() != null, QualityContinuousImprovement::getGroupId, bo.getGroupId());
        lqw.eq(bo.getManagerId() != null, QualityContinuousImprovement::getManagerId, bo.getManagerId());
        lqw.eq(bo.getLeaderId() != null, QualityContinuousImprovement::getLeaderId, bo.getLeaderId());
        return lqw;
    }

    /**
     * 新增持续改善
     */
    @Override
    public Boolean insertByBo(QualityContinuousImprovementBo bo) {
        QualityContinuousImprovement add = BeanUtil.toBean(bo, QualityContinuousImprovement.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改持续改善
     */
    @Override
    public Boolean updateByBo(QualityContinuousImprovementBo bo) {
        QualityContinuousImprovement update = BeanUtil.toBean(bo, QualityContinuousImprovement.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(QualityContinuousImprovement entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除持续改善
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
